{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Repoducing Table 9 from the paper Constrained Risk Budgeting Portfolios."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from pyrb import ConstrainedRiskBudgeting\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "vol = [0.05,0.05,0.07,0.1,0.15,0.15,0.15,0.18]\n",
    "cor = np.array([[100,  80,  60, -20, -10, -20, -20, -20],\n",
    "               [ 80, 100,  40, -20, -20, -10, -20, -20],\n",
    "               [ 60,  40, 100,  50,  30,  20,  20,  30],\n",
    "               [-20, -20,  50, 100,  60,  60,  50,  60],\n",
    "               [-10, -20,  30,  60, 100,  90,  70,  70],\n",
    "               [-20, -10,  20,  60,  90, 100,  60,  70],\n",
    "               [-20, -20,  20,  50,  70,  60, 100,  70],\n",
    "               [-20, -20,  30,  60,  70,  70,  70, 100]])/100\n",
    "cov = np.outer(vol,vol)*cor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "solution x: [26.8306 28.6769 11.4095  9.7985  5.6135  5.9029  6.656   5.1121]\n",
      "lambda star: 4.7776\n",
      "risk contributions: [12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5]\n",
      "sigma(x): 4.7776\n",
      "sum(x): 100.0\n",
      "mu(x): 0.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "C = None\n",
    "d = None\n",
    "\n",
    "CRB = ConstrainedRiskBudgeting(cov,C=C,d=d)\n",
    "CRB.solve()\n",
    "print(CRB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "solver: admm_ccd\n",
      "----------------------------\n",
      "solution x: [25.7859 27.4087  9.5153  7.2904  7.0579  7.7127  9.2265  6.0027]\n",
      "lambda star: 3.5906\n",
      "risk contributions: [ 8.6357  8.6355  8.6356  8.6356 15.9089 16.5836 18.1435 14.8215]\n",
      "sigma(x): 5.1974\n",
      "sum(x): 100.0\n",
      "mu(x): 0.0\n",
      "C@x: [-0.29999813]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "C = np.array([[0,0,0,0,-1.0,-1.0,-1.0,-1.0]]) \n",
    "d = [-0.3]\n",
    "\n",
    "CRB = ConstrainedRiskBudgeting(cov,C=C,d=d)\n",
    "CRB.solve()\n",
    "print(CRB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "solver: admm_ccd\n",
      "----------------------------\n",
      "solution x: [24.5238 28.6911  9.5175  7.2674  6.9707  7.8033  9.2305  5.9956]\n",
      "lambda star: 3.5783\n",
      "risk contributions: [ 8.1646  9.1316  8.6103  8.6103 15.6929 16.8225 18.1567 14.8111]\n",
      "sigma(x): 5.1947\n",
      "sum(x): 100.0\n",
      "mu(x): 0.0\n",
      "C@x: [-0.30000182 -0.05000006]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "C = np.array([[0,0,0,0,-1.0,-1.0,-1.0,-1.0],[1,-1,0,0,1,-1,0,0]]) \n",
    "d = [-0.3,-0.05]\n",
    "\n",
    "CRB = ConstrainedRiskBudgeting(cov,C=C,d=d)\n",
    "CRB.solve()\n",
    "print(CRB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "solver: admm_ccd\n",
      "----------------------------\n",
      "solution x: [ 0.1575 69.2533  0.3023  0.2868 29.4809  0.2412  0.1871  0.091 ]\n",
      "lambda star: 0.1833\n",
      "risk contributions: [ 2.6239  2.6173  2.6239  2.6239 80.2721  3.259   3.1166  2.8634]\n",
      "sigma(x): 5.1158\n",
      "sum(x): 100.0\n",
      "mu(x): 9.3581\n",
      "C@x: [-0.30000095 -0.39855982]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "C = np.array([[0,0,0,0,-1.0,-1.0,-1.0,-1.0],[1,-1,0,0,1,-1,0,0]]) \n",
    "d = [-0.3,-0.05]\n",
    "pi = [-0.1,0.05,0,0,0.2,0.1,0,-0.1]# not in the paper\n",
    "c = 2\n",
    "\n",
    "CRB = ConstrainedRiskBudgeting(cov,C=C,d=d,pi=pi,c=c)\n",
    "CRB.solve()\n",
    "print(CRB)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
